package com.jykj.modbus.manage;

import com.jykj.modbus.equipment.entity.CollectInformation;
import com.jykj.modbus.utils.MathUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 空气传感器
 */
public class AirSensor {

    /**
     * 解析返回的报文信息
     * @param hexStr 报文信息
     * @param type 返回结果类型(10:温湿度;20:PM2.5;30:PM10;)
     * @return
     */
    public static Map<String , Object> messageConversion(String hexStr , String type){
        Map<String , Object> mapData = new HashMap<>();
        List<CollectInformation> humitureData = new ArrayList<CollectInformation>();
        //解析地址码
        mapData.put("addressID" , hexStr.substring(0,2));
        //解析功能码
        mapData.put("functionID" , hexStr.substring(2,4));
        //有效字节数
        mapData.put("significantByte" , hexStr.substring(4,6));
        if (type.equals("101")){
            //湿度
            CollectInformation collect2 = new CollectInformation();
            Double humidity = (MathUtils.hex16To10(hexStr.substring(6, 10)))*0.1;
            collect2.setTypeName("湿度");
            String roundedHumData = MathUtils.rounded(humidity);
            collect2.setValue(roundedHumData);
            collect2.setUnit("%RH");
            //湿度类型码
            collect2.setType("111");
            humitureData.add(collect2);
            //温度
            CollectInformation collect1 = new CollectInformation();
            collect1.setTypeName("温度");
            Double value = (MathUtils.hex16To10(hexStr.substring(10, 14)))*0.1;
            String roundedData = MathUtils.rounded(value);
            collect1.setValue(roundedData);
            collect1.setUnit("摄氏度");
            //温度类型码
            collect1.setType("121");
            humitureData.add(collect1);
            mapData.put("CollectInformation" , humitureData);
            //解析校验码低位
            mapData.put("checkLow" , hexStr.substring(14,16));
            //解析校验码高位
            mapData.put("checkHigh" , hexStr.substring(16,18));
        }else if (type.equals("102")){
            //PM2.5
            CollectInformation collect1 = new CollectInformation();
            Double pm25 = MathUtils.hex16To10(hexStr.substring(6, 10));
            collect1.setTypeName("PM2.5");
            collect1.setValue(pm25.toString());
            collect1.setUnit("微克每立方");
            collect1.setType("102");
            humitureData.add(collect1);
            mapData.put("CollectInformation" , humitureData);
            //解析校验码低位
            mapData.put("checkLow" , hexStr.substring(10,12));
            //解析校验码高位
            mapData.put("checkHigh" , hexStr.substring(12,14));
        }else if (type.equals("103")){
            //PM10
            CollectInformation collect1 = new CollectInformation();
            Double pm10 = MathUtils.hex16To10(hexStr.substring(6,10));
            collect1.setTypeName("PM10");
            collect1.setValue(pm10.toString());
            collect1.setUnit("微克每立方");
            collect1.setType("103");
            humitureData.add(collect1);
            mapData.put("CollectInformation" , humitureData);
            //解析校验码低位
            mapData.put("checkLow" , hexStr.substring(10,12));
            //解析校验码高位
            mapData.put("checkHigh" , hexStr.substring(12,14));
        }
        return mapData;
    }

//    public static void main(String[] args) {
//        AirSensor airSensor = new AirSensor();
//        Map<String, Object> map = airSensor.messageConversion("010304014A00AF9A65", "101");
//        System.out.println(map);
//    }

}
